package com.example.send.config;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;

import java.time.LocalTime;

/**
 * 公共添加消息头信息
 */
public class CompositeMessagePostProcessor implements MessagePostProcessor {

    public static final MessagePostProcessor INSTANCE = new CompositeMessagePostProcessor();
    private MessagePostProcessor traceableMessagePostProcessor;


    @Override
    public Message postProcessMessage(Message message) throws AmqpException {
        message = addTraceId(message);
        return addHead(message);
    }

    private Message addTraceId(Message message) {
        return traceableMessagePostProcessor.postProcessMessage(message);
    }

    private Message addHead(Message message) {
        message.getMessageProperties().getHeaders().put("date", LocalTime.now());
        return message;
    }

    private CompositeMessagePostProcessor(){
        this.traceableMessagePostProcessor = TraceableMessagePostProcessor.INSTANCE;
    }

}
